	<?php
/**
* @version		$Id: dynl_structure.php 1 2007-01-11 22:03:46Z friesengeist $
* @package		Dynlist
* @copyright	Copyright (C) 2005 - 2007 Dynlist. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Dynlist is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
/**
* Dynlist structure class
*
* Manage the structure creation, edition or deletion.
*
* @package		Dynlist
* @final
*/
class Dynl_Structure
{
	/**
	 * Structure of Dynlist.
	 *
	 * @var array
	 * @access protected
	 */
	var $_structure = null;

	/**
	* Dynlist name.
	*
	* @var char
	* @access protected
	*/
	var $_dynlname = null;

        /**
	 * Database instance.
	 *
	 * @var object db_catalog object
	 * @access private
	*/
	var $_database;

	/**
	 * HTML instance.
	 *
	 * @var object html_catalog object
	 * @access private
	*/
	var $_html;

	/**
	* Class constructor
	*
	* @access protected
	* @param
	*/
	function dynl_structure($dynlname)
	{
                $this->_dynlname = $dynlname;
                $this->_database = new dynl_database('fieldcat');
	        $this->_html = new html_data();

		// Define filter.
		$filter = array('dbtabname',$this->_dynlname);
		
		// Define orderby.
		$orderby = array('fieldorder');
		
		// Get information about structure.
		$this->_structure = $this->_database->retrieve('all',$filter,$orderby);
		//echo 'check1';
		//print_r($this->_structure);
	}

	/**
	* Returns the fields names of Dynlist.
	*
	* @access public
	* @return structure
	*/
	function get_fieldnames()
	{
		$fieldnames = array();
		// get the column index for fieldname.
		$fieldname_key = array_search('fieldname',$this->_structure[0]);
		
		for ($i = 1; $i <= (count($this->_structure) - 1); $i++)
		{
			$record = $this->_structure[$i];
			array_push($fieldnames,$record[$fieldname_key]); 
		}
		
		return $fieldnames;
	}
	
	/**
	* Returns the field texts of structure of Dynlist.
	*
	* @access public
	* @return structure
	*/
	function get_texts()
	{
		// get the column index for short text and long text.
		$text_s_key = array_search('text_s',$this->_structure[0]);
		$text_l_key = array_search('text_l',$this->_structure[0]);
		$texts = array();
		
		// loop starts at 1 because index 0 is the field names.
		for ($i = 1; $i <= (count($this->_structure) - 1); $i++){
			$record = $this->_structure[$i];
			$text_rec = array ($record[$text_s_key],$record[$text_l_key]); 
			array_push($texts,$text_rec); 
		}
		//$record = $this->_structure[1];
		//echo $record[$text_s_key].' - '.$record[$text_l_key];
		//print_r($record);	
		//print_r($texts);
		return $texts;
	}

	/**
	* Invocated the HTML render to user create new structure.
	*
	* @access public
	*/
	function add_structure()
	{
		
	}

	/**
	 * Return the keys list
	 *
 	 * @access public
	*/
	function get_keyfields()
	{
                // Define fields.
		$fields = array('fieldname');	

		// Define filter.
                $filter = array('dbtabname',$this->_dynlname,'f_key','x');

                // Define orderby.
                $orderby = array('fieldorder');

                // Get information about structure.
                $keys = $this->_database->retrieve($fields,$filter,$orderby);

		// Convert data array from retrieve method to an array with list of fields.
		$j = 0;
		for ($i = 1; $i < count($keys); $i++)
		{
			$key = $keys[$i];
			$keyfields[$j] = $key[0];
			$j++;
			
		}
		//echo 'keys------>';
		//print_r($keys[0]);
		return $keyfields;

	}

        /**
	 * Return a list of all index of fields that are part of key.
	 *
	 * @access proteced
	 */
	function get_keyindexes()
	{
		// Initialize $indexes array.
	
		// This array has the list of all index fields that are keys.
		$indexes = array();
		//echo 'data------->';
		//print_r($this->_structure[0]);
		
		//echo 'keyfields---->';
		//print_r($this->get_keyfields());
		
		// Get index in line array at data array for fields that are keys.
		foreach ($this->_structure[0] as $fieldname)
		{
			// Index of actual field.
			$index = array_search($fieldname,$this->get_keyfields());

			// If actual field name is an interger, it means that it's a key.
			if (is_int($index))
			{
				// Include fieldname in indexes array.
				array_push($indexes,$index);
			}
		}
		//echo 'indexes----->';
		//print_r($indexes);
		return $indexes;
	}

	/**
	 * Get key field values
	 *
	 * @access protected
	 */
	//function get_keyvalues()
	//{
		
//	} 

	/**
	* Manage the creation of new structure.
	*
	* @access public
	*/
	function new_structure()
	{
		
	}

	/**
	* Manage the creation of new structure.
	*
	* @access public
	*/
	function edit_structure()
	{
		
	}

	/**
	* Delete strucure of Dynlist.
	*
	* @access public
	*/
	function delete_structure()
	{

	}

	/**
	* Add field to structure. Invocates HTML render to show fields for new field.
	*
	* @access public
	*/
	function add_field()
	{

	}

	/**
	* Edit existing field of structure. Invocates HTML render to show field information
	* for user change.
	*
	* @access public
	*/
	function edit_field()
	{

	}

	/**
	* Delete field. Invocates HTML render to show structure without deleted field.
	*
	* @access public
	*/
	function delete_field()
	{

	}

	/**
	* Save structure (new or edited) on database. Invocated HTML render to show Dynlist Catalog.
	*
	* @access public
	*/
	function save_strucure()
	{
		
	}
	
	/**
	* Ignore all changes on structure. Invocated HTML render to show Dynlist Catalog.
	*
	* @access public
	*/
	function cancel()
	{
		
	}
	
}
?>
